package com.example.teamtribe.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.teamtribe.R;
import com.example.teamtribe.utils.Utility;

import butterknife.ButterKnife;

public class BaseActivity extends AppCompatActivity implements BaseView {

    //private LoadingDialog loadingDialog;
    private View progress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            getWindow().setStatusBarColor(Color.WHITE);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        View back = findViewById(R.id.iv_back);
        if (back != null) {
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        ButterKnife.bind(this);
        initProgress();
    }

    @Override
    public void showToast(final String meg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Utility.makeToast(meg, Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress != null) {
                    progress.setVisibility(View.VISIBLE);
                }
                //loadingDialog = new LoadingDialog();
                //loadingDialog.show(getSupportFragmentManager(), "loading");
            }
        });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress != null) {
                    progress.setVisibility(View.GONE);
                }
                //loadingDialog.dismiss();
            }
        });
    }

    public void initProgress() {
        View view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        if (view == null || !(view instanceof ViewGroup)) {
            return;
        }
        ViewGroup viewGroup = (ViewGroup) view;
        progress = LayoutInflater.from(this).inflate(R.layout.loading, viewGroup, false);
        if (progress != null) {
            viewGroup.addView(progress);
            progress.bringToFront();
            progress.setOnClickListener(null);
        }
    }
}
